package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Client;
import com.ruoyi.system.domain.SysClientAss;
import com.ruoyi.system.mapper.ClientMapper;
import com.ruoyi.system.service.IClientService;
import com.ruoyi.system.service.ISysClientAssService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-01
 */
@Service
public class ClientServiceImpl implements IClientService 
{
    @Resource
    private ClientMapper clientMapper;

    @Resource
    private ISysUserService sysUserService;

    @Override
    public Long selectClientCount(Client client) {
        Long aLong = clientMapper.selectClientCount(client);
        return aLong;
    }

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Client selectClientById(Long id)
    {
        return clientMapper.selectClientById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param client 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<Client> selectClientList(Client client)
    {
        return clientMapper.selectClientList(client);
    }

    @Resource
    private ISysClientAssService sysClientAssService;

    /**
     * 新增【请填写功能名称】
     * 
     * @param client 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public Long insertClient(Client client, Long userId)
    {
        client.setCreateTime(DateUtils.getNowDate());
        int rel = clientMapper.insertClient(client);
        if (rel <= 0) {
            return 0L;
        } else {
            //添加分配记录
            SysClientAss sysClientAss = new SysClientAss();
            sysClientAss.setIsAss(1L);
            sysClientAss.setcId(client.getId());
            if (StringUtils.isBlank(client.getAssId())) {
                sysClientAss.setUserId(userId);
            } else {
                sysClientAss.setUserId(Long.valueOf(client.getAssId()));
            }
            sysClientAss.setCreateBy(client.getCreateUserId());
            sysClientAss.setCreateTime(new Date());
            int i = sysClientAssService.insertSysClientAss(sysClientAss);
            if (i <= 0) {
                throw new ServiceException("fail,please again");
            }
            return client.getId();
        }
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param client 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int updateClient(Client client, String userName)
    {
        client.setUpdateTime(DateUtils.getNowDate());
        int i = clientMapper.updateClient(client);
        if (i <= 0) {
            throw new ServiceException("fail,please again");
        }
        if (!StringUtils.isBlank(client.getType()) &&
                client.getType().equals("1")) {
            //把之前的数据isAss状态改为0
            SysClientAss sysClientAss = new SysClientAss();
            sysClientAss.setcId(client.getId());
            List<SysClientAss> sysClientAssList = sysClientAssService.selectSysClientAssList(sysClientAss);
            if (sysClientAssList != null && sysClientAssList.size() > 0) {
                for (SysClientAss ass : sysClientAssList) {
                    if (ass.getIsAss().longValue() == 1) {
                        ass.setIsAss(0L);
                        sysClientAssService.updateSysClientAss(ass);
                    }
                }
            }
            //插入新数据
            SysClientAss assNew = new SysClientAss();
            assNew.setIsAss(1L);
            assNew.setCreateTime(new Date());
            assNew.setCreateBy(userName);
            assNew.setcId(client.getId());
            assNew.setUserId(Long.valueOf(client.getAssId()));
            int i1 = sysClientAssService.insertSysClientAss(assNew);
            if (i1 <= 0) {
                throw new ServiceException("fail,please again");
            }
        }
        return 1;
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteClientByIds(Long[] ids)
    {
        return clientMapper.deleteClientByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteClientById(Long id)
    {
        return clientMapper.deleteClientById(id);
    }
}
